package com.zygh.lz.controller.app;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zygh.lz.constant.SystemCon;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.entity.Equipage;
import com.zygh.lz.entity.SignEquipage;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.entity.StaffEquipage;
import com.zygh.lz.entity.vo.EquipageVO;
import com.zygh.lz.service.EquipageTypeService;
import com.zygh.lz.service.StaffEquipageService;
import com.zygh.lz.service.impl.EquipageServiceImpl;
import com.zygh.lz.service.impl.SignEquipageServiceImpl;
import com.zygh.lz.stapprove.*;
import com.zygh.lz.util.DateUtils;
import com.zygh.lz.vo.ResultBeans;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Api(tags = "装备绑定")
@Slf4j
@RestController
public class AppEquipageController {

    @Resource
    private EquipageServiceImpl equipageService;
    @Resource
    private StaffMapper staffMapper;

    @Resource
    private SignEquipageServiceImpl signEquipageService;

    @Resource
    private StaffEquipageService staffEquipageService;

    @Resource
    private EquipageTypeService equipageTypeService;

    @ApiOperation("根据警号查询装备")
    @PostMapping("getEquipageById")
    @ViLog(logType = "1", module = "根据警号查询装备")
    public OutBean getEquipageById(@RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        String code = jsonObject.getString("code");
        ResultBeans equipageById = equipageService.getEquipageById(code);
        return OutBeanUtil.resSuc(equipageById, inBean);

    }


    @ApiOperation("新增插入绑定装备")
    @PostMapping("insertEquipage")
    public OutBean insert(@RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        Equipage equipage = new Equipage();
//        equipage.setGpsid(jsonObject.getString("gpsId"));
//        equipage.setType(jsonObject.getString("type"));
//        equipage.setCarNo(jsonObject.getString("carNo"));
        equipage.setWarningSignal(jsonObject.getString("warningSignal"));
//        equipage.setDeptName(jsonObject.getString("deptName"));
//        equipage.setRemark2(jsonObject.getString("remark2"));
//        equipage.setCreatTime(new Date());
        String qrcode = jsonObject.getString("qrcode");
        qrcode = StrUtil.cleanBlank(qrcode);
        qrcode = StrUtil.removeAllLineBreaks(qrcode);
        equipage.setQrcode(qrcode);
        ResultBeans resultBeans = equipageService.policeEquipageBind(equipage);

        return OutBeanUtil.resSuc(resultBeans, inBean);
    }

    @ApiOperation("装备解绑")
    @PostMapping("unbindingEquipment")
    public OutBean unbindingEquipment(@RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        boolean remove = staffEquipageService.remove(Wrappers.<StaffEquipage>lambdaQuery()
                .eq(StaffEquipage::getGpsid, jsonObject.getString("gpsid"))
                .eq(StaffEquipage::getPoliceNumber, jsonObject.getString("warningSignal")));
        if (remove) {
            return OutBeanUtil.resSuc(ResultUtil.resultBeansSuccess("解绑成功"), inBean);
        } else {
            return OutBeanUtil.resSuc(ResultUtil.resultBeansError("解绑失败，未查询到装备", null), inBean);
        }
    }


    @ApiOperation("根据二维码查询装备信息")
    @PostMapping("getEquipageByQrcode")
    @ViLog(logType = "1", module = "根据二维码查询装备信息")
    public OutBean getEquipageByQrcode(@RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        String qrcode = jsonObject.getString("qrcode");
        qrcode = StrUtil.cleanBlank(qrcode);
        qrcode = StrUtil.removeAllLineBreaks(qrcode);
        EquipageVO equipageVO = equipageService.getEquipageVOByQrcode(qrcode);
        if (org.springframework.util.StringUtils.isEmpty(equipageVO)) {
            return OutBeanUtil.resSuc(ResultUtil.resultBeansError("未查询到装备", null), inBean);
        }
        return OutBeanUtil.resSuc(ResultUtil.resultBeansSuccess(equipageVO), inBean);

    }

    @ApiOperation("根据二维码查询装备信息本地测试")
    @PostMapping("getEquipageByQrcodeTest")
    @ViLog(logType = "1", module = "根据二维码查询装备信息本地测试")
    public OutBean getEquipageByQrcode(String qrcode) {
        InBean inBean = new InBean();
        EquipageVO equipageVO = equipageService.getEquipageVOByQrcode(qrcode);
        return OutBeanUtil.resSuc(ResultUtil.resultBeansSuccess(equipageVO), inBean);

    }

    @ApiOperation("签到装备绑定")
    @PostMapping("signBindingEquipage")
    public OutBean signBindingEquipage(@RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        String signnum = jsonObject.getString("signnum");
        ArrayList<SignEquipage> equipageArrayList = new ArrayList<>();
        JSONArray gpsIds = jsonObject.getJSONArray("gpsids");
        // 循环获取签到装备 gpsid
        gpsIds.forEach(gpsId -> {
            SignEquipage signEquipage = new SignEquipage();
            signEquipage.setGpsid(gpsId.toString());
            signEquipage.setSignnum(signnum);
            equipageArrayList.add(signEquipage);
        });
        equipageArrayList.forEach(signEquipage -> {
            int count = signEquipageService.count(Wrappers.<SignEquipage>lambdaQuery().eq(SignEquipage::getGpsid, signEquipage.getGpsid())
                    .eq(SignEquipage::getSignnum, signEquipage.getSignnum())
                    .between(SignEquipage::getCreateTime, DateUtils.getDayBegin(), DateUtils.getDayEnd()));
            if (count == 0) {
                signEquipageService.save(signEquipage);
            }
        });
        return OutBeanUtil.resSuc(ResultUtil.resultBeansSuccess("签到装备绑定成功"), inBean);

    }


    @ApiOperation("修改绑定装备")
    @PostMapping("updateEquipage")
    public OutBean update(@RequestBody(required = false) InBean inBean) {
        Equipage equipage = new Equipage();
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        String equipageRadio = jsonObject.getString("EquipageRadio");
        String equipageNumber = jsonObject.getString("EquipageNumber");
        String policeNumber = jsonObject.getString("PoliceNumber");
        equipage.setWarningSignal(policeNumber);
        equipage.setUploadTime(DateUtil.now());
        if (StringUtils.isNotEmpty(equipageRadio)) {
            equipage.setGpsid(equipageRadio);
            ResultBeans resultBeans = equipageService.updateEquipage(equipage);
            return OutBeanUtil.resSuc(resultBeans, inBean);
        }

        if (StringUtils.isNotEmpty(equipageNumber)) {
            equipage.setGpsid(equipageNumber);
            ResultBeans resultBeans = equipageService.updateEquipage(equipage);
            return OutBeanUtil.resSuc(resultBeans, inBean);
        }


//        equipage.setGpsid(jsonObject.getString("gpsId"));
//        equipage.setId(Integer.valueOf(jsonObject.getString("Id")));
//        equipage.setType(jsonObject.getString("type"));
//        equipage.setCarNo(jsonObject.getString("carNo"));
//        equipage.setWarningSignal(jsonObject.getString("warningSignal"));
//        equipage.setDeptName(jsonObject.getString("deptName"));
//        equipage.setRemark2(jsonObject.getString("remark2"));
//        equipage.setUploadTime(DateUtil.now());
        //       ResultBeans resultBeans = equipageService.updateEquipage(equipage);
        return OutBeanUtil.resSuc(null, inBean);
    }


}

